package com.leetcode.tree.recursion;

import com.leetcode.basic.TreeNode;

/**
 * @author Dennis Li
 * @date 2020/10/17 16:05
 */
public class ConstructMaximumBinaryTree_654 {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return nums.length == 0 ? null : constructMaximumBinaryTree(nums, 0, nums.length - 1);
    }

    public TreeNode constructMaximumBinaryTree(int[] nums, int start, int end) {
        if (start > end)
            return null;
        int max = Integer.MIN_VALUE, index = 0;
        for (int i = start; i <= end; i++) {
            if (max < nums[i]) {
                max = nums[i];
                index = i;
            }
        }
        TreeNode node = new TreeNode(max);
         node.left = constructMaximumBinaryTree(nums, start, index - 1);
        node.right = constructMaximumBinaryTree(nums, index + 1, end);
        return node;
    }

    public static void main(String[] args) {
        ConstructMaximumBinaryTree_654 res = new ConstructMaximumBinaryTree_654();
        int[] arr = {3,2,1,6,0,5};
        res.constructMaximumBinaryTree(arr);
    }

}
